package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.form.*;
import com.woniuxy.controller.info.Showpageinof;
import com.woniuxy.controller.info.Warehouseinfo;
import com.woniuxy.dao.PawnShopWhRelDao;
import com.woniuxy.exception.DdException;
import com.woniuxy.exception.ShopCodeCopeExption;
import com.woniuxy.exception.deleShopIsNULL;
import com.woniuxy.model.PawnShopWhRel;
import com.woniuxy.model.PawnWarehouse;
import com.woniuxy.dao.PawnWarehouseDao;
import com.woniuxy.service.PawnShopWhRelService;
import com.woniuxy.service.PawnWarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@Service
public class PawnWarehouseServiceImpl extends ServiceImpl<PawnWarehouseDao, PawnWarehouse> implements PawnWarehouseService {
    @Resource
    private PawnWarehouseDao warehouseDao;
    @Resource
    private PawnShopWhRelDao shopWhRelDao;
    @Override
    public Result findAllPage(FindAllWhpageform findAllWhpageform) {
        Page<PawnWarehouse> allWhpage = baseMapper.selectPage(findAllWhpageform, null);
        List<PawnWarehouse> records = allWhpage.getRecords();//仓库对象集合
        List<Warehouseinfo> warehouseinfoList= NewCopyBean.copyList(records,Warehouseinfo.class);
        ArrayList<Warehouseinfo> showpageinofs1 = new ArrayList<>();//创建一个集合来接收
        Page<Warehouseinfo> showpageinofPage = new Page<>();//返回的分页对象
        for (Warehouseinfo warehouseinfo : warehouseinfoList) {
            List<String> strings = warehouseDao.findWhandShopname(warehouseinfo.getWhCode());
            String s="";
            for (String string : strings) {
                s+=string+",";
            }
            warehouseinfo.setShopName(s);
            showpageinofs1.add(warehouseinfo);
        };
        showpageinofPage.setRecords(showpageinofs1);
        showpageinofPage.setCurrent(allWhpage.getCurrent());
        showpageinofPage.setSize(allWhpage.getSize());
        showpageinofPage.setTotal(allWhpage.getTotal());
        return Result.success(200,"查询成功",showpageinofPage);
    }

    @Override
    public Result findWhpageBylike(FindWhByLikefrom findShopByLikefrom) {
        QueryWrapper<PawnWarehouse> wq = new QueryWrapper<>();
        wq.like("wh_name", findShopByLikefrom.getLikeName())
                .or().like("contact", findShopByLikefrom.getLikeName())
                .or().like("phone_no", findShopByLikefrom.getLikeName());
        Page<PawnWarehouse> WhlistPage = baseMapper.selectPage(findShopByLikefrom, wq);
        List<PawnWarehouse> Whlist = WhlistPage.getRecords();
        System.out.println(Whlist);
        List<Warehouseinfo> warehouseinfos = NewCopyBean.copyList(Whlist, Warehouseinfo.class);
        System.out.println(warehouseinfos);
        ArrayList<Warehouseinfo> warehouseinfos1 = new ArrayList<>();//返回的真实集合
        Page<Warehouseinfo> warehouseinfoPage = new Page<>();//返回的分页对象
        if (null==findShopByLikefrom.getShopStat()||
                findShopByLikefrom.getShopStat().equals("")){
            for (Warehouseinfo warehouseinfo : warehouseinfos) {
                List<String> strings = warehouseDao.findWhandShopname(warehouseinfo.getWhCode());
                String s="";
                for (String string : strings) {
                    s+=string+",";
                }
                warehouseinfo.setShopName(s);
                warehouseinfos1.add(warehouseinfo);
            }
            warehouseinfoPage.setRecords(warehouseinfos1);
            warehouseinfoPage.setCurrent(WhlistPage.getCurrent());
            warehouseinfoPage.setSize(WhlistPage.getSize());
            warehouseinfoPage.setTotal(WhlistPage.getTotal());
            return Result.success(200,"查询成功",warehouseinfoPage);
        }
        //果如加上状态查询
        QueryWrapper<PawnWarehouse> wq1 = new QueryWrapper<>();
        wq1.eq("wh_stat",findShopByLikefrom.getShopStat());
        wq1.and(ww -> ww.
                like("wh_name", findShopByLikefrom.getLikeName()).or().
                like("contact", findShopByLikefrom.getLikeName()).or().
                like("phone_no", findShopByLikefrom.getLikeName()).or());
        Page<PawnWarehouse> WhlistPageStat = baseMapper.selectPage(findShopByLikefrom, wq1);
        List<PawnWarehouse> whlist = WhlistPageStat.getRecords();
        System.out.println(whlist);
        List<Warehouseinfo> warehouseinfoList= NewCopyBean.copyList(whlist,Warehouseinfo.class);
        System.out.println(warehouseinfoList);
        ArrayList<Warehouseinfo> warehouseinfos2 = new ArrayList<>();//返回的真实集合
        Page<Warehouseinfo> warehouseinfoPage1 = new Page<>();//返回的分页对象
        for (Warehouseinfo warehouseinfo : warehouseinfoList) {
            List<String> strings = warehouseDao.findWhandShopname(warehouseinfo.getWhCode());
            String s="";
            for (String string : strings) {
                s+=string+",";
            }
            warehouseinfo.setShopName(s);
            warehouseinfos2.add(warehouseinfo);
        }
        warehouseinfoPage1.setRecords(warehouseinfos2);
        warehouseinfoPage1.setCurrent(WhlistPageStat.getCurrent());
        warehouseinfoPage1.setSize(WhlistPageStat.getSize());
        warehouseinfoPage1.setTotal(WhlistPageStat.getTotal());
        return Result.success(200, "查询成功", warehouseinfoPage1);
    }

    @Override
    public Result deleWhList(DeleWhListfrom deleWhListfrom)throws DdException {
        List<PawnWarehouse> listWh = deleWhListfrom.getListWh();
        if (ObjectUtils.isEmpty(listWh))throw new deleShopIsNULL(5000,"请勾选删除的仓库");
        for (PawnWarehouse pawnWarehouse : listWh) {
            baseMapper.delete(new QueryWrapper<PawnWarehouse>()
                    .eq("wh_code",pawnWarehouse.getWhCode()));//删仓库表的数据
            shopWhRelDao.delete(new QueryWrapper<PawnShopWhRel>().eq("wh_code",
                    pawnWarehouse.getWhCode() ));
        }

        return Result.success(200,"删除成功",null);
    }

    @Override
    public Result addWh(AddWhfrom addWhfrom) throws DdException {
        if (!ObjectUtils.isEmpty(warehouseDao.selectOne(new QueryWrapper<PawnWarehouse>()
                .eq("wh_name",addWhfrom.getWhName()))))  throw new ShopCodeCopeExption(50002,"该仓库已存在重新输入");
        warehouseDao.insert(new PawnWarehouse(null,addWhfrom.getWhName(),addWhfrom.getContact()
                ,addWhfrom.getPhoneNo(),addWhfrom.getAddress()
        ,addWhfrom.getWhStat()));
        if (!ObjectUtils.isEmpty(addWhfrom.getShopCode())){
            PawnWarehouse warehouse = baseMapper.selectOne(new QueryWrapper<PawnWarehouse>()
                    .eq("wh_name", addWhfrom.getWhName()));
            for (Integer s : addWhfrom.getShopCode()) {
                shopWhRelDao.insert(new PawnShopWhRel(s,warehouse.getWhCode()));

            }
        }
        return Result.success(200,"新增成功",null);
    }
}
